package com.tsingteng.cosfun.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.Toast;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.callback.AppListener;
import com.tsingteng.cosfun.common.BroadcastManager;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.http.Url;
import com.tsingteng.cosfun.ui.cosfun.contract.ShareContract;
import com.tsingteng.cosfun.ui.cosfun.presenter.SharePresenter;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;

import org.litepal.util.SharedUtil;

public class ShareUtils implements ShareContract.IShareView {

    public static String shareTitle = "发布了一个超有趣的合演作品，快来看看吧~";
    public static String sharePlayTitle = "发布了一个超有趣的段子，快来看看吧~";
    public static String sharePersonCardStart= "我在烤饭发现一个非常有趣的人。";
    public static String sharePersonCardEnd = "的烤饭名片！";
    public static String sharePersonCardDes = "这里的小哥哥小姐姐那么好看，声音又好听，不喜欢？不存在的！";
    public static String shareDes = "这里的合演很精彩";
    public static String sharePlayDes = "这里的合演很精彩";
    public static String shareVideoUrl = Url.BASE_SHARE_URL+"video?videoId=";
    public static String sharePlayUrl = Url.BASE_SHARE_URL+"paragraph?id=";
    public static String shareUserUrl = Url.BASE_SHARE_URL+"user?profileId=";
    public static String imageUrl = "";
    private ShareListener shareListener;
    private static ShareUtils intance = null;
    private SharePresenter sharePresenter;
    private String playId = null;
    private String videoId = null;
    private String prifitId;
    private Activity mActivity;
    private String mPingUrl="?imageView2/0/w/200";

    private ShareUtils() {

    }


    public static ShareUtils getIntance() {
        if (intance == null) {
            synchronized (ShareUtils.class) {
                intance = new ShareUtils();
            }
        }
        return intance;
    }

    //文本分享
    public void shareTxt(Activity activity, SHARE_MEDIA platform, String content,ShareListener shareListener) {
        new ShareAction(activity).withText(content)
                .setPlatform(platform)
                .setCallback(umShareListener).open();
    }

    //链接分享
    public void shareLink(Activity activity, SHARE_MEDIA platform, String url, String imgUrl, String title, String description) {
        UMImage image;
        Bitmap bitmap =BitmapFactory.decodeResource(activity.getResources(),R.mipmap.ic_launcher);
        if (TextUtils.isEmpty(imgUrl)) {
            image = new UMImage(activity, bitmap);//本地图片
        }else{
            image = new UMImage(activity, imgUrl+mPingUrl);//网络图片
        }
        UMWeb web = new UMWeb(url);
        web.setTitle(title);//标题
        web.setThumb(image);  //缩略图
        web.setDescription(description);//描述
        new ShareAction(activity).withMedia(web)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    /**
     * 增加分享回调,完成的时候调用,上面的分享同理

     */
    //视频分享
    public void shareVideo(Activity activity, SHARE_MEDIA platform, String videoId,String videoUrl, String imgUrl, String title, String description, ShareListener shareListener) {
        this.shareListener = shareListener;
        this.videoId = videoId;
        this.mActivity = activity;
        UMImage image = new UMImage(activity, imgUrl+mPingUrl);//网络图片
        UMVideo video = new UMVideo(shareVideoUrl+videoId);
        video.setTitle(title);//标题
        video.setThumb(image);  //缩略图
        video.setDescription(description);//描述
        new ShareAction(activity).withMedia(video)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    //视频分享
    public void foundShareVideo(Activity activity, SHARE_MEDIA platform, String videoId,String videoUrl, String imgUrl, String title, String description, ShareListener shareListener) {
        this.shareListener = shareListener;
        this.videoId = videoId;
        UMImage image = new UMImage(activity, imgUrl+mPingUrl);//网络图片
        UMVideo video = new UMVideo(videoUrl);
        video.setTitle(title);//标题
        video.setThumb(image);  //缩略图
        video.setDescription(description);//描述
        new ShareAction(activity).withMedia(video)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    /**
     * 增加分享回调,完成的时候调用,上面的分享同理
     */
    //默认分享
    public void shareDefault(Activity activity, SHARE_MEDIA platform, ShareListener shareListener) {
        this.shareListener = shareListener;
        UMWeb web = new UMWeb(sharePlayUrl);
        web.setTitle(shareTitle);//标题

//        web.setThumb(image);  //缩略图
        web.setDescription(shareDes);//描述
        new ShareAction(activity).withMedia(web)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    /**
     * 增加分享回调,完成的时候调用,上面的分享同理
     */
    //段子分享
    public void sharePlay(Activity activity, SHARE_MEDIA platform,String playId,String title,String shareDes, String imageUrl,ShareListener shareListener) {
        this.shareListener = shareListener;
        this.playId = playId;
//        UMImage image = new UMImage(activity, imageUrl);//网络图片
        UMWeb web = new UMWeb(sharePlayUrl+playId);
        web.setTitle(title+sharePlayTitle);//标题
        if (TextUtils.isEmpty(imageUrl)) {
            web.setThumb(new UMImage(activity, R.drawable.logo));  //本地缩略图
        } else {
            web.setThumb(new UMImage(activity, imageUrl+mPingUrl));  //网络缩略图
        }
//        web.setThumb(image);  //缩略图
        web.setDescription(shareDes);//描述
        new ShareAction(activity).withMedia(web)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    /**
     * 增加分享回调,完成的时候调用,上面的分享同理
     */
    //主页分享
    public void sharePerson(Activity activity, SHARE_MEDIA platform,String prifitId,String title,String shareDes, String imageUrl,ShareListener shareListener) {
        this.shareListener = shareListener;
        this.prifitId = prifitId;
        UMWeb web = new UMWeb(shareUserUrl+prifitId);
        web.setTitle(title );//标题
        if (TextUtils.isEmpty(imageUrl)) {
            web.setThumb(new UMImage(activity, R.drawable.logo));  //本地缩略图
        } else {
            web.setThumb(new UMImage(activity, imageUrl+mPingUrl));  //网络缩略图
        }
//        web.setThumb(image);  //缩略图
        web.setDescription(shareDes);//描述
        new ShareAction(activity).withMedia(web)
                .setPlatform(platform)
                .setCallback(umShareListener).share();
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            showLoading("正在分享...");
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            if (shareListener != null) {
                shareListener.onShareFinish();
            }
            sharePresenter = new SharePresenter();
            sharePresenter.attachView(ShareUtils.this);
            if (!TextUtils.isEmpty(playId)) {
                sharePresenter.getXiPuTranspone(playId);
            }
            if (!TextUtils.isEmpty(videoId)) {
                sharePresenter.getVideoTranspone(videoId);
            }
            hideLoading();
            AppListener.OnShareTotalListener shareTotalListener = AppListener.getInstance().getmShareTotalListener();
            if (null != shareTotalListener){
                shareTotalListener.onShareTotal("");
            }
            Toast.makeText(AppContext.getContext(), "分享完成", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            if (shareListener != null) {
                shareListener.onShareFinish();
            }
            hideLoading();
            Toast.makeText(AppContext.getContext(), "分享错误", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            if (shareListener != null) {
                shareListener.onShareFinish();
            }
            hideLoading();
            Toast.makeText(AppContext.getContext(), "分享取消", Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showFail(String msg) {

    }

    @Override
    public void showError() {

    }

    @Override
    public void showEmpty() {

    }


    @Override
    public void showXiPuTranspone(Integer data) {
        playId = null;
//        ToastUtils.showToast("showXiPuTranspone"+ data);

        if (sharePresenter!= null) {
            sharePresenter.detachView();
            sharePresenter.removeAllDisposable();
        }
        if (shareListener != null) {
            shareListener.onShareSuccess();
        }
    }

    @Override
    public void showVideoTranspone(Integer data) {
        videoId = null;
//        ToastUtils.showToast("showVideoTranspone"+ data);
        if (sharePresenter!= null) {
            sharePresenter.detachView();
            sharePresenter.removeAllDisposable();
        }
        if (shareListener != null) {
            shareListener.onShareSuccess();
        }
    }

    public interface ShareListener {
        void onShareFinish();
        void onShareSuccess();
    }
}